<?php
/**
 * 发货通知单推送通知仓库封装类
 *
 * @author xiayuanjun
 * @Time: 2016-04-20
 * @version 0.1
 */

class ome_delivery_notice{

    /**
     * @description 修改配送信息
     * @access public
     * @param int $delivery_id 发货单ID
     * @return void
     */
    static public function create($delivery_id) {

        $operationLogObj = app::get('ome')->model('operation_log');
        $operationLogObj->write_log('delivery_modify@ome',$delivery_id,"发货通知单推送仓库开始",NULL,$opInfo);

        $dlySendObj = app::get('console')->model('delivery_send');
        $dlySendObj->update_send_status($delivery_id,'sending');

        $original_data = kernel::single('ome_event_data_delivery')->generate($delivery_id);
        $wms_id = kernel::single('ome_branch')->getWmsIdById($original_data['branch_id']);

        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        $result = $dlyTriggerLib->create($wms_id, $original_data, false);
        if($result['rsp'] == 'fail'){
            $msg = "失败. 原因:".$result['msg'];
        }else{
            $msg ="成功.";

            if (isset($result['data']['wms_order_code'])) {
                $dlyExtObj = app::get('console')->model('delivery_extension');
                $ext_data['delivery_bn'] = $original_data['outer_delivery_bn'];
                $ext_data['original_delivery_bn'] = $result['data']['wms_order_code'];
                $dlyExtObj->save($ext_data);
            }
        }

        if(isset($result['msg_id'])){
            $msg .=" msg_id:".$result['msg_id'];
        }

        $operationLogObj->write_log('delivery_modify@ome',$delivery_id,"推送结果:".$msg,NULL,$opInfo);
    }

    static public function cancel($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        $dlyObj = app::get('ome')->model("delivery");
        $branchObj = app::get('ome')->model("branch");
        $branch =$branchObj->getList('branch_bn',array('branch_id'=>$params['branch_id']),0,1);
        $data['branch_bn'] =$branch[0]['branch_bn'];

        if(isset($params['wms_id'])){
            $wms_id = $params['wms_id'];
        }else{
            $branchLib = kernel::single('ome_branch');
            $wms_id = $branchLib->getWmsIdById($params['branch_id']);
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->cancel($wms_id, $data, $sync);
    }

    static public function pause($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        if(isset($params['wms_id'])){
            $wms_id = $params['wms_id'];
        }else{
            $branchLib = kernel::single('ome_branch');
            $wms_id = $branchLib->getWmsIdById($params['branch_id']);
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->pause($wms_id, $data, $sync);
    }

    static public function renew($params, $sync = false){

        $dly_id = $params['delivery_id'];
        $data['outer_delivery_bn'] = $params['delivery_bn'];

        if(isset($params['wms_id'])){
            $wms_id = $params['wms_id'];
        }else{
            $branchLib = kernel::single('ome_branch');
            $wms_id = $branchLib->getWmsIdById($params['branch_id']);
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->renew($wms_id, $data, $sync);
    }

    static public function search($params, $sync = false){

        $dly_id = $params['delivery_id'];

        $dlyExtObj = app::get('console')->model('delivery_extension');
        $dlyExtInfo = $dlyExtObj->dump(array('delivery_bn'=>$params['delivery_bn']),'original_delivery_bn');
        $data = array(
            'delivery_bn'=>$params['delivery_bn'],
            'out_order_code'=>$dlyExtInfo['original_delivery_bn'],    
        );

        if(isset($params['wms_id'])){
            $wms_id = $params['wms_id'];
        }else{
            $branchLib = kernel::single('ome_branch');
            $wms_id = $branchLib->getWmsIdById($params['branch_id']);
        }
        
        $dlyTriggerLib = kernel::single('ome_event_trigger_delivery');
        return $dlyTriggerLib->search($wms_id, $data, $sync);
    }
}